package com.nulldev.util.gui.customSwing.resizing;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public abstract class BackgroundComponentDragger implements MouseMotionListener {

	private Component controlledComponent;

	/*
	 * Point where cursor was last clicked.
	 */
	private Point originPoint;

	public BackgroundComponentDragger(Component component) {
		this.controlledComponent = component;
	}

	public abstract boolean skip();

	@Override
	public void mouseDragged(MouseEvent e) {
		if (this.skip()) {
			return;
		}
		Point currentFramePosition = controlledComponent.getLocation();
		Point newFramePosition = new Point(currentFramePosition.x + e.getX() - originPoint.x, currentFramePosition.y + e.getY() - originPoint.y);
		controlledComponent.setLocation(newFramePosition);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if (this.skip()) {
			return;
		}
		originPoint = e.getPoint();
	}
}
